import {createApp} from 'vue'
import Cookies from 'js-cookie'
import ElementPlus from 'element-plus'
import locale from 'element-plus/es/locale/lang/zh-cn'


// 样式文件（注意顺序）
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import '@/assets/styles/index.scss' // 全局样式

// Markdown 编辑器
import VMdEditor from '@kangc/v-md-editor'
import '@kangc/v-md-editor/lib/style/base-editor.css'
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js'
import '@kangc/v-md-editor/lib/theme/style/github.css'

// 应用核心模块
import App from './App'
import store from './store'
import router from './router'
import directive from './directive'
import plugins from './plugins'


// 工具类
import {download} from '@/utils/request'
import {useDict} from '@/utils/dict'
import {
    parseTime,
    resetForm,
    addDateRange,
    handleTree,
    selectDictLabel,
    selectDictLabels
} from '@/utils/ruoyi'

// 组件
import 'virtual:svg-icons-register'
import SvgIcon from '@/components/SvgIcon'
import elementIcons from '@/components/SvgIcon/svgicon'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'
import Editor from "@/components/Editor"
import FileUpload from "@/components/FileUpload"
import ImageUpload from "@/components/ImageUpload"
import ImagePreview from "@/components/ImagePreview"
import DictTag from '@/components/DictTag'
import { GridLayout, GridItem } from 'grid-layout-plus'

// 权限控制
import './permission'

// 初始化应用
const app = createApp(App)

// ================== 全局配置 ==================
// 1. 使用插件
app.use(router)
app.use(store)
app.use(plugins)
app.use(elementIcons)

// 2. ElementPlus 配置
app.use(ElementPlus, {
    locale,
    size: Cookies.get('size') || 'default'
})

// 3. Markdown 编辑器配置
VMdEditor.use(githubTheme)
app.use(VMdEditor)

    // ================== 全局注册 ==================
    .component('GridLayout', GridLayout)
    .component('GridItem', GridItem)
// 1. 注册组件
const components = {
    SvgIcon: {name: 'svg-icon', component: SvgIcon},
    DictTag,
    Pagination,
    FileUpload,
    ImageUpload,
    ImagePreview,
    RightToolbar,
    Editor
}

Object.entries(components).forEach(([key, value]) => {
    if (value.name) {
        app.component(value.name, value.component)
    } else {
        app.component(key, value)
    }
})

// 2. 注册指令
directive(app)

// ================== 全局属性 ==================
const globalProperties = {
    useDict,
    download,
    parseTime,
    resetForm,
    handleTree,
    addDateRange,
    selectDictLabel,
    selectDictLabels
}

Object.entries(globalProperties).forEach(([key, value]) => {
    app.config.globalProperties[key] = value
})

// ================== 挂载应用 ==================
app.mount('#app')